package pl.koziolekweb.szkola.KJChatUDP;

// KJ-ChatUDP/UDPKlient.java
 
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class UDPKlient {

    static DatagramSocket  socout = null;
    static DatagramSocket  sockin = null;
    static InetAddress     addr   = null;
    static String LF = System.getProperty("line.separator");
    static String tmp;
    static byte[] inport;
    static final int PORT    = 54321;
    static final int BUFSIZE = 32*1024;

    public static void main(String[] args) {
        JFrame f = new JFrame("UDPKlient");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        try {
              // moglby byc jeden socket, ale bylyby
              // klopoty z synchronizacja, bo przy
              // odbieraniu socket sie blokuje...
            addr   = InetAddress.getByName("localhost");
            socout = new DatagramSocket();
            sockin = new DatagramSocket();
              // wysylamy numer portu odbiorczego
            inport = (""+sockin.getLocalPort()).getBytes();
            DatagramPacket pout =
                new DatagramPacket(
                    inport,0,inport.length,addr,PORT);
            socout.send(pout);
        } catch(Exception e) {
            e.printStackTrace(); System.exit(1);
        }

        final JTextArea  ta = new JTextArea(30,10);
        final JTextField tf = new JTextField(30);
        tf.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                byte[] mes = null;
                try {
                    mes = tf.getText().getBytes("UTF-8");
                    tf.setText("");
                    DatagramPacket p = new DatagramPacket(
                            mes,0,mes.length,addr,PORT);
                    socout.send(p);
                } catch(Exception a) {
                    a.printStackTrace(); System.exit(1);
                }
            }
        });

        JScrollPane scr = new JScrollPane(ta,
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        f.add(scr,BorderLayout.CENTER);
        f.add(tf,BorderLayout.SOUTH);
        f.pack();
        f.setVisible(true);

          // nieskonczona petla odbierajaca
        DatagramPacket pin =
            new DatagramPacket(new byte[BUFSIZE],BUFSIZE);
        while (true) {
            pin.setLength(BUFSIZE);
            try {
                sockin.receive(pin);
                tmp = new String(
                        pin.getData(),pin.getOffset(),
                        pin.getLength(),"UTF-8");
            } catch(Exception e) {
                e.printStackTrace(); System.exit(1);
            }

            SwingUtilities.invokeLater(
                new Runnable() {
                    public void run() { ta.append(tmp+LF); }
                }
            );
        }
    }
}
